package com.lanou.controller;

import com.lanou.bean.User;
import com.lanou.service.UserService;
import com.lanou.util.AjaxResultUtil;
import com.lanou.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import static org.apache.shiro.web.filter.mgt.DefaultFilter.user;

/**
 * Created by dllo on 18/2/26.
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Resource
    private UserService userService;

    private AjaxResultUtil ajaxResultUtil = new AjaxResultUtil();


//    @ResponseBody
//    @RequestMapping(value = "/login")
//    public boolean Login(@RequestParam(value = "name") String name,
//                         @RequestParam(value = "password") String password,
//                         HttpSession session) {
//        User user = userService.searchUser(name, password);
//        session.setAttribute("user", user);
//        return user != null;
//    }


    @ResponseBody
    @RequestMapping(value = "/register")
    public Boolean register(@RequestParam(value = "name") String name,
                            @RequestParam(value = "password") String password,
                            @RequestParam(value = "tel") String tel,
                            @RequestParam(value = "address") String address) {
        StringUtil stringUtil = new StringUtil();
        name = stringUtil.deleteBlank(name);
        password = stringUtil.deleteBlank(password);

        if (name.equals("") || password.equals("")) {
            return false;
        } else {
            userService.addUser(name, password, tel, address);
            //检测是否注册成功成功返回true失败返回false
            User user = userService.searchUser(name, password);
            return user != null;
        }
    }

    //名字处理
    @ResponseBody
    @RequestMapping(value = "/name")
    public AjaxResultUtil name() {

        Subject subject = SecurityUtils.getSubject();
        String  username = (String) subject.getPrincipal();
        ajaxResultUtil.setResultCode(1);
        ajaxResultUtil.setMessage("获得用户名");
        ajaxResultUtil.setData(username);
        return ajaxResultUtil;
    }

    //博客主页
    @RequestMapping(value = "/blog")
    public String blog() {
        return "blogmain";
    }

}
